/**   
 * @Title: MoreLotteryWebView.java 
 * @Package com.esun.rabbit2.viewer.mainact.lotteryhallact 
 * @Description: 更多彩种跳转到3g的网页  
 * @author: Billy Qigs@500wan.com   
 * @date: 2012-3-8 下午5:08:07  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.lotteryhallact;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.DistributionChannels;
import com.esun.config.Language;
import com.esun.ui.R;

/**
 * @ClassName: MoreLotteryWebView
 * @Description: 更多彩种跳转到3g的网页
 * @author: Billy Qigs@500wan.com
 * @date: 2012-3-8 下午5:08:07
 * 
 */
public class MoreLotteryWebView extends Activity implements OnClickListener {
	private WebView webView;
	// 下一页按钮
	private ImageButton btnGoAhead;
	// 前一页按钮
	private ImageButton btnGoBack;
	// 刷新按钮
	private ImageButton btnRefresh;
	// 主页按钮
	private ImageButton btnHome;
	// 返回按钮
	private ImageButton btnBack;
	// 加载用背景
	private RelativeLayout relativeProgress;

	private String url;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_lottery_webview_act);

		initUI();
	}

	// ui初始化
	private void initUI() {

		initWebView();

		btnBack = (ImageButton) findViewById(R.id.more_lottery_webview_btn_cancel);
		btnBack.setOnClickListener(this);

		btnGoAhead = (ImageButton) findViewById(R.id.main_btn_goAhead);
		btnGoAhead.setOnClickListener(this);

		btnGoBack = (ImageButton) findViewById(R.id.main_btn_goBack);
		btnGoBack.setOnClickListener(this);

		btnRefresh = (ImageButton) findViewById(R.id.main_btn_refresh);
		btnRefresh.setOnClickListener(this);

		btnHome = (ImageButton) findViewById(R.id.main_btn_home);
		btnHome.setOnClickListener(this);

		relativeProgress = (RelativeLayout) findViewById(R.id.more_lottery_web_act_progress);

	}

	// webview初始化
	private void initWebView() {
		webView = (WebView) findViewById(R.id.main_webview_3g);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(WebView view, String url) {
				super.onPageFinished(view, url);
				relativeProgress.setVisibility(View.GONE);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				super.onPageStarted(view, url, favicon);
				relativeProgress.setVisibility(View.VISIBLE);
			}
		});

		if (DistributionChannels.LEAGUENUM.equals(Config.DEFAULT_LEAGUAGE_NUM)) {
			url = Config.URL_3G_HOME;
		} else {
			url = Config.URL_3G_HOME + "/base_index?coopid=" + DistributionChannels.LEAGUENUM + "&cid=" + DistributionChannels.QUDAO;
		}

		webView.loadUrl(url);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		// 点击下一页
		case R.id.main_btn_goAhead:

			if (webView.canGoForward()) {
				webView.goForward();
			} else {
				// 提示没有下一页
				Toast.makeText(MoreLotteryWebView.this, Language.TIP_ALREADY_LAST_PAGE, Toast.LENGTH_SHORT).show();
			}

			break;

		// 点击前一页
		case R.id.main_btn_goBack:

			if (webView.canGoBack()) {
				webView.goBack();
			} else {
				// 提示没有前一页
				Toast.makeText(MoreLotteryWebView.this, Language.TIP_ALREADY_FIRST_PAGE, Toast.LENGTH_SHORT).show();
			}

			break;

		// 点击刷新
		case R.id.main_btn_refresh:

			// 重新加载当前页
			webView.reload();

			break;
		// 点击主页
		case R.id.main_btn_home:

			// 重新加载首页
			webView.loadUrl(url);

			break;

		// 点击返回
		case R.id.more_lottery_webview_btn_cancel:

			MoreLotteryWebView.this.finish();

			break;

		default:

			break;

		}

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (webView.canGoBack()) {
				webView.goBack();
			} else {
				finish();
				return true;
			}
		}

		return super.onKeyDown(keyCode, event);
	}

}
